package com.example.poidemo.poi;

import com.alibaba.fastjson.JSON;
import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlCursor;
import org.junit.Test;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.*;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.List;

/**
 * @Description TODO
 * @Author lililiang
 * @Date 21/03/31 10:10
 * @Version 1.0
 * XWPF 用于处理docx文件（word 2007+)
 **/
public class XWPFDocumentUtil {
    public static String path = "Output.docx";
    public static String pathBack = "./test1-back.docx";


    public static void replace(String path, String pathBack, String regex, String replacement) throws Exception {

        InputStream is = new FileInputStream(path);
        XWPFDocument doc = new XWPFDocument(is);
        //读取文本
        List<XWPFParagraph> paragraphs = doc.getParagraphs();
        for (XWPFParagraph p : paragraphs) {
            String text = p.getText();
            if (text.equals(regex)) {
                XWPFRun run = p.getRuns().get(0);
                run.addBreak();
                run.setText(replacement);
                CTShd cTShd = run.getCTR().addNewRPr().addNewShd();
                cTShd.setVal(STShd.CLEAR);
                cTShd.setFill("97FFFF");
            }
        }
        FileOutputStream out = new FileOutputStream(pathBack);
        doc.write(out);

        closeStream(out);
        closeStream(is);

        is = new FileInputStream(pathBack);
        doc = new XWPFDocument(is);
        paragraphs = doc.getParagraphs();
        for (XWPFParagraph p : paragraphs) {
            System.out.println("p: " + p.getParagraphText());
        }
        closeStream(is);
    }

    /**
     * @return void
     * @Description 删除文件中包含指定字符串的段落
     * @Author lililiang
     * @Date 09:49 21/04/01
     * @Param [path, regex]
     **/
    public static void removeParagraph(String path, String regex) throws Exception {
        // 读取两次文件，单文件处理会出现并发问题
        InputStream is = new FileInputStream(path);
        XWPFDocument doc = new XWPFDocument(is);
        closeStream(is);
        is = new FileInputStream(path);
        XWPFDocument docTemp = new XWPFDocument(is);
        closeStream(is);
        //读取文本，删除指定段落
        Iterator<XWPFParagraph> pi = doc.getParagraphsIterator();
        while (pi.hasNext()) {
            XWPFParagraph next = pi.next();
            String text = next.getText();
            if (text.contains(regex)) {
                docTemp.removeBodyElement(doc.getPosOfParagraph(next));
            }
        }
        // 将删除后的内容写回文件
        FileOutputStream out = new FileOutputStream(path);
        docTemp.write(out);
        closeStream(out);
    }

    public static void main(String[] args) throws Exception {
//        replace(path, pathBack, "嘻嘻哈哈冒菜加盟优势", "我是插入的哦，嘻嘻哈哈冒菜加盟优势");
        removeParagraph(path, "Evaluation Warning: The document was created with Spire.Doc for JAVA.");
    }

    /**
     * 关闭输入流
     *
     * @param is
     */
    private static void closeStream(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭输出流
     *
     * @param os
     */
    private static void closeStream(OutputStream os) {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
